package com.xiaoliu.dao.impl;


import com.xiaoliu.bean.User;
import com.xiaoliu.dao.UserDao;
import com.xiaoliu.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    boolean bo = false;
    int affectedRows = 0;

    //    public UserDaoImpl() {
//        con = JDBCUtil.getCon();
//    }
    @Override
    public User login(String username, String password) {
        con = JDBCUtil.getCon();
        User login = null;

        if (con != null) {
            // 判断数据库是否连接成功
            System.out.println("login--数据库连接成功~");
            String sql = "select * from users where username = ? and pwd = ?";
            try {
                ps = con.prepareStatement(sql);
                ps.setString(1, username);
                ps.setString(2, password);
                rs = ps.executeQuery();
                // 解析结果集
                while (rs.next()) {
                    login = new User();
                    login.setUid(rs.getInt("uid"));
                    login.setUsername(rs.getString("username"));
                    login.setPassword(rs.getString("pwd"));
                    login.setEmail(rs.getString("email"));
                    login.setSex(rs.getString("sex"));
                }


            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JDBCUtil.close(rs, ps, con);
            }

        } else {
            System.out.println("数据库连接失败!");
        }
        return login;
    }

    @Override
    public boolean userExists(String username) {
        con = JDBCUtil.getCon();
        if (con != null) {
            System.out.println("userExists--数据库连接成功~");
            String sql = "select * from users where username = ? ";
            try {
                ps = con.prepareStatement(sql);
                ps.setString(1, username);
                rs = ps.executeQuery();
                while (rs.next()) {
                    // 如果有数据，bo为true，结束循环
                    bo = true;
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JDBCUtil.close(rs, ps, con);
            }
        } else {
            System.out.println("数据库连接失败！");
        }
        return bo;// 返回boolean值，判断是否有这个用户
    }

    @Override
    public boolean userTrue(String username, String email) {
        con = JDBCUtil.getCon();
        if (con != null) {
            System.out.println("userTrue--数据库连接成功~");
            String sql = "select * from users where username = ? and email = ?";
            try {
                ps = con.prepareStatement(sql);
                ps.setString(1, username);
                ps.setString(2, email);
                rs = ps.executeQuery();
                while (rs.next()) {
                    bo = true;
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JDBCUtil.close(rs, ps, con);
            }
        } else {
            System.out.println("数据库连接失败！");
        }
        return bo;
    }

    @Override
    public boolean register(String user, String pwd, String email, String sex) {
        con = JDBCUtil.getCon();
        if (!userExists(user)) {
            if (con != null) {
                System.out.println("register--数据库连接成功~");
                String sql = "insert into users(username,pwd,email,sex) values(?,?,?,?) ";
                try {
                    ps = con.prepareStatement(sql);
                    ps.setString(1, user);
                    ps.setString(2, pwd);
                    ps.setString(3, email);
                    ps.setString(4, sex);
                    affectedRows = ps.executeUpdate();// 受影响的行数赋值给affectedRows
                    if (affectedRows > 0) {
                        bo = true;// 大于0则有行数受影响 数据库发生了改变 数据添加成功
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    JDBCUtil.close(ps, con);
                }

            } else {
                System.out.println("数据库连接失败！");
            }
        }
        return bo;// 返回boolean值 判断数据是否添加成功
    }


    @Override
    public boolean change(String user, String pwd, String email) {
        con = JDBCUtil.getCon();
        if (con != null) {
            System.out.println("change--数据库连接成功~");
            String sql = "update users set pwd = ? where username = ? and email = ?";
            try {
                ps = con.prepareStatement(sql);
                ps.setString(1, pwd);
                ps.setString(2, user);
                ps.setString(3, email);
                affectedRows = ps.executeUpdate();
                if (affectedRows > 0) {
                    bo = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JDBCUtil.close(ps, con);
            }
        } else {
            System.out.println("数据库连接失败！");
        }

        return bo;
    }

}